 <template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />


        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('user.mobile') + '/' + t('user.nickname') })"
        />

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'

defineOptions({
    name: 'warehouse/warehousePosition',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['weigh-sort', 'edit', 'delete'])

interface Props {
    parent_id:number
}
const props = withDefaults(defineProps<Props>(), {
    parent_id: 0,
})

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/user.User/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70 },
            // { label: t('user.User name'), prop: 'username', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('user.nickname'), prop: 'nickname', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('user.parent_id'), prop: 'parent_id', align: 'center', operator: 'eq'},
            { label: t('user.p_nickname'), prop: 'puser.nickname', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            // {
            //     label: t('user.grouping'),
            //     prop: 'group.name',
            //     align: 'center',
            //     operator: 'LIKE',
            //     operatorPlaceholder: t('Fuzzy query'),
            //     render: 'tag',
            // },
            { label: t('user.head portrait'), prop: 'avatar', align: 'center', render: 'image', operator: false },
            // {
            //     label: t('user.Gender'),
            //     prop: 'gender',
            //     align: 'center',
            //     render: 'tag',
            //     custom: { '0': 'info', '1': '', '2': 'success' },
            //     replaceValue: { '0': t('Unknown'), '1': t('user.male'), '2': t('user.female') },
            // },
            { label: t('user.mobile'), prop: 'mobile', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            // { label: t('user.coin_num'), prop: 'coin_num', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.coin_money'), prop: 'coin_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.back_buy_money'), prop: 'back_buy_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.directly_return_money'), prop: 'directly_return_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.push_money'), prop: 'push_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.shop_money'), prop: 'shop_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.shareholder_money'),width:110, prop: 'shareholder_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.ten_money'),width:110, prop: 'ten_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.hundred_money'),width:110, prop: 'hundred_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // {
            //     label: t('user.Last login IP'),
            //     prop: 'last_login_ip',
            //     align: 'center',
            //     operator: 'LIKE',
            //     operatorPlaceholder: t('Fuzzy query'),
            //     render: 'tag',
            // },
            // {
            //     label: t('user.Last login'),
            //     prop: 'last_login_time',
            //     align: 'center',
            //     render: 'datetime',
            //     sortable: 'custom',
            //     operator: 'RANGE',
            //     width: 160,
            // },
            // {
            //     label: t('user.vip_status'),
            //     prop: 'vip_status',
            //     align: 'center',
            //     render: 'tag',
            //     custom: { 0: 'danger', 1: 'success' },
            //     replaceValue: { 0: t('user.vip_status 0'), 1: t('user.vip_status 1') },
            // },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: { disable: 'danger', enable: 'success' },
                replaceValue: { disable: t('Disable'), enable: t('Enable') },
            },
            { label: t('Create time'), prop: 'create_time', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },

            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined, 'status'],
        defaultOrder: { prop: 'weigh', order: 'desc' },
    },
    {
        defaultItems: { status: 'enable', parent_id:props.parent_id},
    },{
        getIndex:function () {
            baTable.table.filter!.search = baTable.getComSearchData()
            baTable.table.filter!.search.push({
                field: 'parent_id', // 筛选字段
                val: props.parent_id,
                operator: '=',
            })
            // 刷新表格
            // baTable.onTableHeaderAction('refresh', { event: 'com-search', data: baTable.table.filter!.search })

        },
        onTableAction({ event, data }) {
            if (event == 'com-search') {
                baTable.table.filter!.search = baTable.getComSearchData()
                baTable.table.filter!.search.push({
                    field: 'parent_id', // 筛选字段
                    val: props.parent_id,
                    operator: '=',
                })
                // 刷新表格
                baTable.onTableHeaderAction('refresh', { event: 'com-search', data: baTable.table.filter!.search })
                return false;
            }
        },
    },{

    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.mount()
    baTable.getIndex()
})
</script>

<style scoped lang="scss"></style>
